<template>
  <div class="contain-logon">
    <div class="logon-form">
      <div class="login-header">电子报告智能化管理平台</div>
      <div class="login-body">
        <el-form ref="loginForm" :model="form" label-width="0px" :rules="rules">
          <el-form-item prop="phone">
            <el-input placeholder="可使用手机号/学号注册" maxlength="11" v-model="form.phone">
              <i slot="prefix" class="el-input__icon el-icon-mobile-phone"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" show-password placeholder="请输入密码" v-model="form.password"
                      style="margin-top: 20px">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="truePassword">
            <el-input type="password" show-password placeholder="再次确认密码" v-model="form.truePassword"
                      style="margin-top: 20px">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="login-btn">
        <el-button type="primary" style="width: 100%" @click="userLogon('loginForm')">注 册</el-button>
      </div>
      <div class="login-btn">
        <el-button type="text" style="width: 100%" @click="getInLogin">返回登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {studentLogon} from "../api/aboutLogin";

export default {
  name: "logon",
  data() {
    // 验证确认密码
    let checkRePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    };
    return {
      form: {
        phone: null,
        password: null,
        truePassword: null
      },
      rules: {
        phone: [{required: true, message: '请输入学号或手机号', trigger: 'blur'}],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 18, message: "长度在 6 到 18个字符", trigger: "blur"},
        ],
        truePassword: [{required: true, trigger: 'blur', validator: checkRePass}],
      },
    }
  },
  methods: {
    //注册
    userLogon(loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          studentLogon(this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.message)
              this.form = {}
              this.$router.replace('/')
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //返回登录
    getInLogin() {
      this.$router.replace('/')
    }
  }
}
</script>

<style scoped lang="less">
.contain-logon {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: url("../assets/loginImg/logonBanner.png") no-repeat center;
  background-size: 100%;

  .logon-form {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 20vw;
    background-color: #ffffff;
    border-radius: 10px;
    box-shadow: 0 0 10px 1px #333;
    padding: 2%;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: space-between;

    .login-header {
      width: 100%;
      text-align: center;
      font-size: 24px;
      letter-spacing: 1px;
      color: #999;
    }

    .login-body {
      width: 100%;
      margin: 10% 0;

      /deep/ .el-form-item__content {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }

      /deep/ .el-form-item {
        margin-bottom: 0;
      }
    }

    .login-btn {
      width: 100%;
      margin: 0 auto;
    }
  }
}
</style>
